#!/bin/sh -e
#
# /etc/init.d/tigase-server -- startup script for tigase-server 4.1.x
#
### BEGIN INIT INFO
# Provides:	  tigase-server
# Required-Start:    $network
# Required-Stop:     $network
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tigase Server
# Description:       Start Tigase Jabber/XMPP Server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=tigase-server
DESC="Tigase Jabber/XMPP Server"
TIGASE_HOME=/usr/share/tigase
TIGASE_RUN="tigase.server.XMPPServer"
TIGASE_JAR="$TIGASE_HOME/lib/tigase-server.jar"

# The following variables can be overwritten in /etc/default/tigase-server

# Whether to start tigase-server (as a daemon) or not
NO_START=0

# Run Tigase as this user ID (default: tigase-server)
# Set this to an empty string to prevent Tigase from starting automatically
TIGASE_USER=tigase
			  
# Database drivers loaded to server						      
TIGASE_DBDRV="-Djdbc.drivers=com.mysql.jdbc.Driver:org.postgresql.Driver:org.apache.derby.jdbc.EmbeddedDriver"

# Path to config file
# It will be generated from init.properties, if absent
TIGASE_CONFIG="/etc/tigase/tigase.xml"

# File for the tigase-server stdout/stderr output
TIGASE_CONSOLE_LOG="/var/log/tigase/tigase-console.log"

# Additional server options
TIGASE_OPTIONS="--property-file /etc/tigase/init.properties"

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in /etc/default/tigase-server)
JDK_DIRS="
	  /usr/lib/jvm/java-6-sun \
	  /usr/lib/j2sdk1.6-sun \
	 "

# End of variables that can be overwritten in /etc/default/tigase-server
										
# overwrite settings from default file
if [ -f /etc/default/tigase-server ]; then
	. /etc/default/tigase-server
fi

# Find tigase-server jar
for jar in $TIGASE_HOME/lib/tigase-server*.jar ; do
	if [ -f $jar ] ; then
	  TIGASE_JAR=$jar
	  break
	fi
done

# Check whether tigase-server is still installed (it might not be if this package was
# removed and not purged)
if [ -r "$TIGASE_JAR" ]; then
    HAVE_TIGASE=1
else
    exit 0
fi

# Check whether startup has been disabled
if [ "$NO_START" != "0" -a "$1" != "stop" ]; then 
	[ "$VERBOSE" != "no" ] && echo "Not starting $DESC - edit /etc/default/tigase-server and change NO_START to be 0 (or comment it out).";
	exit 0;
fi

if [ -z "$TIGASE_USER" ]; then
	echo "Not starting/stopping $DESC as configured (TIGASE_USER is"
	echo "empty in /etc/default/tigase-server)."
	exit 0
fi

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
	if [ -d "$jdir" -a -z "${JAVA_HOME}" ]; then
		JAVA_HOME="$jdir"
	fi
done
export JAVA_HOME

export JAVA="$JAVA_HOME/bin/java"

if [ -z "$JAVA_OPTIONS" ]; then
	JAVA_OPTIONS="-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 \
		      -server -Xms100M -Xmx200M \
		      -Djava.library.path=/usr/lib"
fi
export JAVA_OPTIONS
										
# Define other required variables
PIDFILE="/var/run/$NAME.pid"
LOGDIR="/var/log/tigase"

##################################################
# Check for JAVA_HOME and JRE version
##################################################
if [ -z "$JAVA_HOME" ] || ! "$JAVA" -version 2>&1 | grep -q 'java version[^0-9]\+\([2-9]\|1\.[6-9]\)'; then
	if [ -z "$JAVA_HOME" ]; then
		echo "Could not start $DESC because no Java Runtime Environment (JRE) was found."
	else
		echo "$DESC needs Sun Java(TM) SE Runtime Environment at least version 1.6."
	fi
	echo "Please download and install JRE 1.6 or higher and set"
	echo "JAVA_HOME in /etc/default/tigase-server to the JRE's installation directory."
	exit 0
fi

# Put all jars in classpath
CLASSPATH="$TIGASE_JAR"

CLASSPATH="`ls -d $TIGASE_HOME/lib/*.jar 2>/dev/null | grep -v wrapper | tr '\n' :`$CLASSPATH"
for lib in /usr/lib/jvm/java-6-sun/db/lib/derby.jar \
           /usr/share/java/postgresql.jar \
           /usr/share/java/mysql.jar \
           /usr/share/java/bcprov.jar ; do
  if [ -r $lib ]; then CLASSPATH="$CLASSPATH:$lib"; fi
done

ARGUMENTS="$JAVA_OPTIONS $TIGASE_DBDRV -cp $CLASSPATH $TIGASE_RUN -c $TIGASE_CONFIG $TIGASE_OPTIONS"

##################################################
# Do the action
##################################################
case "$1" in
  start)
	if start-stop-daemon --quiet --test --start --pidfile "$PIDFILE" \
			     --user "$TIGASE_USER" --exec "$JAVA" > /dev/null; then 

	    if [ -f $PIDFILE ]; then
		echo "$PIDFILE exists, but $NAME was not running. Ignoring $PIDFILE"
	    fi

	    echo -n "Starting $DESC: "
		touch $TIGASE_CONSOLE_LOG
		chown $TIGASE_USER:adm $TIGASE_CONSOLE_LOG
		echo "STARTED Tigase `date`" >>$TIGASE_CONSOLE_LOG
		start-stop-daemon --quiet --start --background --pidfile "$PIDFILE" --make-pidfile \
		                  --chuid "$TIGASE_USER" --startas "$TIGASE_HOME/scripts/std-logger.sh" \
		                  -- "$TIGASE_CONSOLE_LOG" "$JAVA" $ARGUMENTS
		sleep 1
		if [ -e "$TIGASE_CONFIG" ]; then
			chown $TIGASE_USER:adm $TIGASE_CONFIG
			chmod 0640 $TIGASE_CONFIG
		fi
		echo "$NAME."
	else
	echo "(already running)."
		exit 1
	fi
	;;

  stop)
  	echo -n "Stopping $DESC: "

	if start-stop-daemon --quiet --test --start --pidfile "$PIDFILE" \
		--user "$TIGASE_USER" --exec "$JAVA" > /dev/null; then
		if [ -x "$PIDFILE" ]; then
			echo "(not running but $PIDFILE exists)."
		else
			echo "(not running)."
		fi
	else
		start-stop-daemon --quiet --stop \
			--pidfile "$PIDFILE" --user "$TIGASE_USER" --exec "$JAVA" > /dev/null
		sleep 1
		while ! start-stop-daemon --quiet --test --start \
			--pidfile "$PIDFILE" --user "$TIGASE_USER" \
			--startas "$JAVA" > /dev/null; do 
			sleep 1
			echo -n "."
			start-stop-daemon --oknodo --quiet --stop \
				--pidfile "$PIDFILE" --user "$TIGASE_USER" --exec "$JAVA"
		done
		rm -f "$PIDFILE"
		echo "$NAME."
	fi
	;;

  restart|force-reload)
	$0 stop $*
	sleep 1
	$0 start $*
	;;

  check)
	echo "Checking arguments to Tigase: "
	echo
	echo "PIDFILE         =  $PIDFILE"
	echo "JAVA_OPTIONS    =  $JAVA_OPTIONS"
	echo "JAVA            =  $JAVA"
	echo "TIGASE_USER     =  $TIGASE_USER"
	echo "ARGUMENTS       =  $ARGUMENTS"
	echo "TIGASE_HOME     =  $TIGASE_HOME"
	echo "TIGASE_JAR      =  $TIGASE_JAR"
	echo "TIGASE_CONFIG   =  $TIGASE_CONFIG"
	echo "TIGASE_OPTIONS  =  $TIGASE_OPTIONS"
	echo "CLASSPATH       =  $CLASSPATH"
	echo "TIGASE_CONSOLE_LOG  =  $TIGASE_CONSOLE_LOG"
	
	if [ -f $PIDFILE ] && kill -0 `cat $PIDFILE` 2>/dev/null
	then
		echo "Tigase running pid="`cat $PIDFILE`
		exit 0
	fi
	exit 1
	;;

  zap)
	rm -f $TIGASE_PID
	;;

  *)
	echo "Usage: /etc/init.d/tigase-server {start|stop|restart|force-reload|check}" >&2
	exit 1
	;;
esac

exit 0
